<template>
  <div class="container">
    <h3 class="title">登录论坛</h3>
    <el-form :model="loginForm" class="login-form">
      <el-form-item>
        <el-input prefix-icon="UserFilled" v-model="loginForm.loginName" />
      </el-form-item>

      <el-form-item >
        <el-input prefix-icon="Key" type="password" v-model="loginForm.password" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" style="width: 100%;" @click="handleLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref} from "vue";
import { useRouter } from "vue-router";

import { useStore } from "vuex";

import request from "@/request/request";

import { ElMessage } from 'element-plus'
import {login} from  "@/api/user"

const router = useRouter();
const store = useStore();

let loginForm = reactive({loginName:"",password:""});

let handleLogin=()=>{
  login(loginForm).then((resp)=>{
    if(resp.code == 200){
      let userInfo = resp.data.userInfo;

      store.commit("initUserInfo",userInfo)

      sessionStorage.setItem("token",resp.data.token);
      sessionStorage.setItem("userInfo",JSON.stringify(userInfo));

      router.replace({"path":"/user/list"});
    }else{
      ElMessage.error(resp.msg)
    }
  }).catch((error)=>{
    alert(error.msg);
  });
}

</script>

<style scoped>
    .container{
      padding-top: 160px;
      width: 100%;
      height:100%;
      box-sizing: border-box;
      background-image: url("./../assets/bg_login.jpeg");
      background-repeat: no-repeat;
    }

    .container .title{
      font-size: 30px;
      color: #fff;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: 500;
    }

    .login-form{
      position: relative;
      width: 400px;
      max-width: 100%;
      margin: 0 auto;
      overflow: hidden;
    }
</style>